package cn.sc.summer.data.config;

import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.kafka.core.ProducerFactory;
import org.springframework.kafka.support.ProducerListener;

/**
 * @author a-xin
 * @description kafka配置
 * @belongsProject SummerCloud
 * @belongsPackage cn.sc.summer.data.config
 * @date 2025-02-21  11:14
 */
@Slf4j
@Configuration
public class KafkaConfig {

    @Bean
    @ConditionalOnBean(ProducerFactory.class)
    public KafkaTemplate<String, Object> kafkaTemplate(ProducerFactory<String, Object> producerFactory) {
        KafkaTemplate<String, Object> kafkaTemplate = new KafkaTemplate<>(producerFactory);
        kafkaTemplate.setProducerListener(new ProducerListener<String, Object>() {
            @Override
            public void onSuccess(ProducerRecord<String, Object> producerRecord, RecordMetadata recordMetadata) {
                log.info("发送成功 " + producerRecord.toString());
            }
        });
        return kafkaTemplate;
    }

}

